package com.tubeonfire.service;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.google.appengine.api.datastore.Text;
import com.google.gdata.client.youtube.YouTubeQuery;
import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.data.youtube.ChannelEntry;
import com.google.gdata.data.youtube.ChannelFeed;
import com.google.gdata.data.youtube.UserProfileEntry;
import com.google.gdata.data.youtube.VideoEntry;
import com.google.gdata.data.youtube.VideoFeed;
import com.google.gdata.util.ServiceException;
import com.tubeonfire.entity.Tube;
import com.tubeonfire.entity.Channel;
import com.tubeonfire.model.TubeModel;

public class ChannelService {

	private static YouTubeService service = new YouTubeService("tubeonfire");

	public static List<Channel> getMostViewdToday() {
		List<Channel> result = null;
		service.setConnectTimeout(2000);
		try {
			YouTubeQuery query = new YouTubeQuery(
					new URL(
							"https://gdata.youtube.com/feeds/api/channelstandardfeeds/most_viewed?time=all_time&max-results=10&orderby=viewCount&v=2"));
			ChannelFeed channels = service.query(query, ChannelFeed.class);
			result = new ArrayList<Channel>();
			for (ChannelEntry channel : channels.getEntries()) {
				Channel tubChannel = new Channel();
				String[] splittedId = channel.getId().split(":");
				tubChannel
						.setTitle(new Text(channel.getTitle().getPlainText()));
				tubChannel.setAlias(splittedId[splittedId.length - 1]);
				UserProfileEntry profileEntry = service.getEntry(new URL(
						channel.getAuthors().get(0).getUri()),
						UserProfileEntry.class);
				tubChannel.setThumbImageUrl(new Text(profileEntry
						.getThumbnail().getUrl()));
				result.add(tubChannel);
			}
		} catch (MalformedURLException e) {
			System.out.println("Error when get most view channel.");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("Error when get most view channel.");
			e.printStackTrace();
		} catch (ServiceException e) {
			System.out.println("Error when get most view channel.");
			e.printStackTrace();
		}
		return result;
	}

	public static Channel getByUserId(String alias) {
		Channel tubChannel = new Channel();		
		List<Text> listTube = new ArrayList<Text>();
		List<Tube> listSave2DB = new ArrayList<Tube>();
		String feedUrl = "http://gdata.youtube.com/feeds/api/users/" + alias
				+ "/uploads";
		service.setConnectTimeout(2000);
		VideoFeed videoFeed;
		try {
			videoFeed = service.getFeed(new URL(feedUrl), VideoFeed.class);
			for (VideoEntry videoEntry : videoFeed.getEntries()) {
				Tube tub = new Tube();
				tub.transformHalfVideoEntry(videoEntry);
				tub.setStatus(2);
				tub.setUpdated(Calendar.getInstance().getTime());
				listTube.add(new Text(tub.toString())); 
				System.out.println(tub.getTubeId());
				if (TubeModel.getByTubeId(tub.getTubeId()) == null) {
					listSave2DB.add(tub);
					System.out.println(tub.getTitle() + " ready to save !");
				}
				
			}
			if (listSave2DB.size() > 0) {
				TubeModel.addAll(listSave2DB);
				System.out.println("Saved " + listSave2DB.size()
						+ " video success !");
			}
			tubChannel.setListTube(listTube);
		} catch (MalformedURLException e) {
			System.out.println("Error when get channel by id : " + alias);
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("Error when get channel by id : " + alias);
			e.printStackTrace();
		} catch (ServiceException e) {
			System.out.println("Error when get channel by id : " + alias);
			e.printStackTrace();
		}

		return tubChannel;
	}

}
